<?php
namespace Idiom\Controller;
use  Common\Controller\IdiomController;
//版本2
class RevisionController extends IdiomController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     * 广告
     */
    public function advertising(){

        $appid = I('post.appid','','htmlspecialchars');

        $ad = M('idiom_appshow')->field('appid,url,envVersion,logo,isshow,category,location')->select();

        foreach ($ad as $key=>$value){

            $ad[$key]['logo'] = $this->url.$ad[$key]['logo'];
        }

        $this->returnData($ad);

    }

    /**
     * 排行
     */
    public function ranking(){

        $member_id = I('post.member_id','','int');
        $state = I('post.state','','int');
        $rank = "";
        //本周排行
        if ($state=='1'){

            $rank = $this->getcacheweek2();
            $this->returnData($rank,'成功','200');
        }

        //好友排行
        if ($state=='2'){

            $where['my_member_id'] = $member_id;

            $coupling_uids = M('idiom_relationship')->where($where)->getField('coupling_member_id',true);
            if (empty($coupling_uids)){
                $coupling_uids = array();
            }
            $coupling_uids[] = $member_id;


            $uids = M('idiom_relationship')->where(array('coupling_member_id'=>$member_id))->getField('my_member_id',true);
            if (empty($uids)){
                $uids = array();
            }

            $ids = array_unique(array_merge($coupling_uids,$uids));//合并去重

            $where1['idiom_member_id'] = array('in',$ids);

            if ($ids){
                $rank = M('idiom_member')->field('nickname,avatarurl,customs')->where($where1)->limit(100)->order('customs desc')->select();
                $this->returnData($rank,'成功','200');
            }



        }

    }

    /**
     * 微信群分享看答案
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');

        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];


        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }

        $info = M('idiom_member')->field('appid,session_key')->where(array('idiom_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];

        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'){
            $this->returnData('','',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['idiom_member_id'] = $member_id;
        $where['openGId'] = $openGId;


        $record = M('idiom_wechat_group')->where($where)->getField('sharenum'); //判断是否已分享


        if ($record>=3){
            $this->returnData(array('repetition'=>'out'),'同群有效分享次数为5',201);
        }

        //如果已分享则自增分享次数然后返回
        if ($record){
            M('idiom_wechat_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'no'),'分享成功',200);
        }


        $data = array(
            'openGId'=>$openGId,
            'sharetime'=>$time,
            'sharenum'=>1,
            'idiom_member_id'=>$member_id,
        );

        M('idiom_wechat_group')->data($data)->add();

        $this->returnData(array('repetition'=>'no'),'分享成功',200);
    }



    /**
     * 获取题目进行挑战
     */
    public function chang(){

        $member_id = I('post.member_id','','int');
        $state = I('post.state','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }
        //一次取20题
        $nextcustoms = 20;

        if ($state=='2'){
            $sort = I('post.sort','','int');
            if (!$sort){
                $this->returnData('','参数错误',401);
            }

            $question = M('idiom_questions')->limit($sort,$nextcustoms)->order('sort asc')->select();

            foreach ($question as $key=>$value){

                $question[$key]['imgsrc'] = $this->url.$question[$key]['imgsrc'];
            }

            $this->returnData($question);

        }

        $customs = M('idiom_member')->where(array('idiom_member_id'=>$member_id))->getField('customs');



        $question = M('idiom_questions')->limit($customs,$nextcustoms)->order('sort asc')->select();

        foreach ($question as $key=>$value){

            $question[$key]['imgsrc'] = $this->url.$question[$key]['imgsrc'];
        }

        $count = M('idiom_questions')->count();

        $this->returnData($question,$count);

    }


    /**
     * 答题
     */
    public function inanswer(){

        $member_id = I('post.member_id','','int');

        $state = I('post.state','','int');

        if (!$member_id||!$state){
            $this->returnData('','参数错误',401);
        }

        $nowweek = (int)date('W');
        $year = date('Y');

        $where = array(
            'year'=>$year,
            'week'=>$nowweek,
            'idiom_member_id'=>$member_id,
        );

        //判断当周是否有挑战记录
        $ischange = M('idiom_week')->field('idiom_week_id')->where($where)->find();
        if ($ischange){
            M('idiom_week')->where($where)->setInc('customs');//当周分数加一

            M('idiom_week')->where($where)->save(array('exittime'=>date('Y-m-d H:i:s')));
        }else{
            $where['createtime'] = date('Y-m-d H:i:s');
            $where['exittime'] = date('Y-m-d H:i:s');
            $where['customs'] = 1;
            M('idiom_week')->data($where)->add();
        }
        M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('customs'); //分数加一

        $this->returnData('','成功',200);

    }


    /**
     * 好友关联
     */
    public function friendsassociated(){

        $uid = I('post.my_member_id','','int');
        $coupling_uid = I('post.coupling_member_id','','int');
        $appid = I('post.appid','','htmlspecialchars');

        if ($uid==""||$coupling_uid==""){
            $this->returnData('','参数不能为空',401);
        }

        if ($uid==$coupling_uid){
            $this->returnData('','自己不用关联自己',100);
        }


        $where['my_member_id']=$uid;
        $where['coupling_member_id']=$coupling_uid;
        $where['appid'] = $appid;

        $where1['my_member_id']=$coupling_uid;
        $where1['coupling_member_id']=$uid;
        $where1['appid'] = $appid;

        $relevancy = M('idiom_relationship')->field('idiom_relationship_id')->where($where)->find();

        $relevancy1 = M('idiom_relationship')->field('idiom_relationship_id')->where($where1)->find();
        //判断是否已关联(有待优化)
        if ($relevancy||$relevancy1){
            $this->returnData('','已关联',200);
        }

        $data = array(
            'my_member_id'=>$uid,
            'coupling_member_id' =>$coupling_uid,
            'appid'=>$appid,
            'createtime' =>time(),
        );

        $insert = M('idiom_relationship')->data($data)->add();

        if ($insert){
            $this->returnData('','关联成功',200);
        }else{
            $this->returnData('','关联失败',100);
        }

    }


}